<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>日志列表</title>
	<link href="${basePath }/static/plugins/layui/css/layui.css" rel="stylesheet"/>
	<link href="${basePath}/static/build/css/app.css" rel="stylesheet" media="all">
</head>
<body>
<div class="" id="log-main" style="margin: 12px;">
	<div class="layui-row">
		<div class="layui-col-xs8">
			<div class="layui-btn-group " id="toolbar">
				<button title="刷新" class="layui-btn layui-btn-primary" data-type="refresh"><i class="layui-icon">&#x1002;</i></button>
			</div>
		</div>
		<div class="layui-col-xs4">
			<div id="search-inputs"></div>
		</div>
	</div>
	<div id="table" lay-filter="log-table"></div>
</div>

<script src="${basePath }/static/plugins/layui/layui.js"></script>
<script src="${basePath }/static/plugins/jq/jquery-2.2.4.min.js"></script>
<script src="${basePath }/static/build/js/common.js"></script>
<script>
    layui.use(['form', 'table', 'layer', 'element', 'search'], function() {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer,
            search = layui.search;


        var tableIns = table.render({
            elem: '#log-main #table'
            ,height: 'full'
            ,url: '${basePath}/admin/log/query' //数据接口
            ,page: true //开启分页
            ,limit: 10
            ,cols: [[ //表头
                {field: 'logId', title: 'ID', sort: true}
                ,{field: 'message', title: '消息'}
                ,{field: 'clasz', title: '类名'}
                ,{field: 'method', title: '方法'}
                ,{field: 'params', title: '参数'}
                ,{field: 'host', title: 'IP' }
                ,{field: 'exctime', title: '耗时(s)', }
                ,{field: 'ctime', title: '操作时间',
                    templet: function (item) {
						return getDateFromMill(item.ctime);
                    }
				}
                ,{field: 'userId', title: '操作人',
                    templet: function (item) {
                    	var user = item.user;
                    	if (user != null) {
                            return item.user.username;
						}
                        return "/";
                    }
                }
            ]]
        });

        //初始化检索组件
        search.render({
            tableIns: tableIns,  //表格对象，必须
            elem: '#search-inputs',  //搜索的div的id或class，必须
            searchName: 'param',  //模糊查询文本框的name
            placeholder: '模糊查询',
            more: true,  //是否需要更多筛选，可选，默认为true，true/false
            fields: [  //更多筛选中的选项，目前仅支持文本框，其余需layui渲染的组件尚未测试，可选
                {name: 'message', type: 'text', title: '消息', placeholder: '请输入消息'}  //name：input的name，type：input的type，title：显示的名称，placeholder：input的提示文字
                ,{name: 'method', type: 'text', title: '方法', placeholder: '请输入方法'}
                ,{name: 'clasz', type: 'text', title: '昵称', placeholder: '请输入类名'}
            ],
            init: function(){
                console.log('初始化后的回调函数');
            },
            onInputChange: function (data) {
                console.log('模糊查询文本框文字改变事件')
                console.log(data);
            },
            onInputSubmit: function (data) {
                console.log('模糊查询按钮点击事件');
                console.log(data);
            },
            onInputEnter: function (data) {
                console.log('模糊查询文本框enter事件');
                console.log(data);
            },
            onMoreShow: function(){
                console.log('更多筛选显示后的回调函数')
            },
            onMoreHide: function(){
                console.log('更多筛选隐藏后的回调函数')
            },
            onMoreSubmit: function (data) {
                console.log('更多筛选确认后的回调函数')
                console.log(data);
            },
            onMoreReset: function (data) {
                console.log('更多筛选重置后的回调函数')
                console.log(data);
            }
        });

        //监听单元格编辑
        table.on('edit(log-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            var reqData = {};
            reqData[field] = value;
            reqData['logId'] = data.logId;
            action.update(reqData);
        });

        var action = {
            deleteBatch: function(){ //批量删除
                var checkStatus = table.checkStatus('log-table')
                    ,data = checkStatus.data;
                if(data.length == 0){
                    return;
                }
                var ids = [];
                for(var i = 0; i < data.length; i++ ){
                    ids.push(data[i].logId);
                }

                deleteAction(ids);
                tableIns.reload();
            },
            refresh: function(){
                tableIns.reload();
            },
            toInsert: function(){
                var options = {
                    url: "${basePath }/admin/log/toinsert",
                    icon: "&#xe658;",
                    title: "新增日志",
                    id: "log-insert"
                }
                parent.tab.tabAdd(options);
            },
            delete: function(ids){
                $.ajax({
                    url: '${basePath}/admin/log/delete',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: ids
                    },
                    success: function(resp){
                        if(resp.code==10000){
                            layer.msg('删除成功',{
                                time: 1000
                            }, function(){
                                //tableIns.reload();
                            })
                        }
                    }
                })
            },
            update: function(reqData){
                $.ajax({
                    url: '${basePath}/admin/log/update',
                    type: 'post',
                    dataType: 'json',
                    data:reqData,
                    success: function(resp){
                        console.log(resp)
                        if(resp.code==10000){
                            layer.msg('更新成功',{
                                time: 1000
                            });
                        }
                    }
                })
            }
        };

        $('#log-main #toolbar .layui-btn').on('click', function(){
            var type = $(this).data('type');
            action[type] ? action[type].call(this) : '';
        });

        action.refresh();
    });


</script>
</body>
</html>